package com.hero.herocat;

import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.io.InputStream;

/**
 * @author linhui
 */
public class ServerResource {
    Document doc;

    ServerResource() {
        try {
            InputStream is = getClass().getClassLoader().getResourceAsStream("server.xml");
            SAXReader saxReader = new SAXReader();
            doc = saxReader.read(is);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("can not parse server.xml");
        }
    }


    public String getString(String name) {
        Node node = doc.selectSingleNode("//"+name);
        return node == null ? null : node.getText();
    }

    public String getString(String name, String defaultValue) {
        String value = getString(name);
        return value == null ? defaultValue : value;
    }

    public Integer getInt(String name) {
        String value = getString(name);
        return value == null ? null : Integer.valueOf(value);
    }

    public Integer getInt(String name, Integer defaultValue) {
        Integer value = getInt(name);
        return value == null ? defaultValue : value;
    }
}
